//
//  VisitorLoginView.swift
//  SinaWeibo
//
//  Created by 王佳琳 on 16/2/16.
//  Copyright © 2016年 王佳琳. All rights reserved.
//

import UIKit
//需要导入命名空间
import SnapKit

//定义协议
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    
    //声明可选的协议方法 使用 optional关键字(是OC中的关键字)
    optional func userWillLogin()
    
    //注册的协议方法
    func userWillRegister()

}
class VisitorLoginView: UIView {
    
    //声明代理对象  注意 在声明代理对象一定要使用 weak 防止循环引用
    weak var visitorLoginViewDelegate: VisitorLoginViewDelegate?

    
    //自定义访客视图
    //默认的构造方法
    override init(frame: CGRect) {
        //调用父类的构造方法
        super.init(frame: frame)
        setupUI()
    }
    
    
    //一旦重写了 init(frame方法之后 系统会默认该视图会通过手写代码创建
    //系统就不会再让同构xib加载视图
    //通过xib加载的UIView及其子类会调用这个方法
    required init?(coder aDecoder: NSCoder) {
        //系统默认在这个地方执行报错
        fatalError("init(coder:) has not been implemented")
        //super.init(coder: aDecoder)
    }
    
    //点击事件的监听方法
    @objc private func registerBtnDidClick() {
        //使用代理调用协议方法
        //OC中调用使用代理调用协议方法  需要做两重判断 1.代理对象不为空  2.代理对象是否响应协议方法
        visitorLoginViewDelegate?.userWillRegister()
    }
    
    @objc private func loginBtnDidClick() {
        //双重判断 1.代理对象不为空  2.代理对象是否响应协议方法
        visitorLoginViewDelegate?.userWillLogin?()
    }
    



    //设置页面信息的方法
    func setupInfoWtih(tipText: String,imageName: String?) {
        tipLable.text = tipText
        if imageName != nil {
            //一定不是首页
            circleIcon.image = UIImage(named: imageName!)
            //隐藏大房子
            largeHouse.hidden = true
            //将circleIcon 移动到最顶层
            self.bringSubviewToFront(circleIcon)
        } else {
            //一定在首页
            startAnimation()
        }
    }
    //大圆圈转动方法
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //重复次数
        anim.repeatCount = MAXFLOAT
        anim.toValue = 2 * M_PI
        //设置动画事件
        anim.duration = 20
        //当动画效果完成 / 视图处于非活跃状态下 动画效果不移除图层
        anim.removedOnCompletion = false
        //将基础动画添加到图层
        circleIcon.layer.addAnimation(anim, forKey: nil)
    }

    //给视图添加约束
    private func setupUI() {
        addSubview(circleIcon)
        addSubview(backImage)
        addSubview(largeHouse)
        addSubview(tipLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //给大圆圈添加约束
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        //给大房子
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        

        //lable
        tipLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.centerY.equalTo(circleIcon.snp_bottom).offset(16)
            
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        
        //设置按钮的约束
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLable.snp_left)
            make.width.equalTo(100)
            make.top.equalTo(tipLable.snp_bottom).offset(16)
            
        }
        
        //设置注册按钮的约束
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLable.snp_right)
            make.width.equalTo(100)
            make.top.equalTo(tipLable.snp_bottom).offset(16)
            
        }
        
        //设置背景图片的约束
        backImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        let value: CGFloat = 236.0 / 255.0
        //let value: CGFloat = 232.0 / 255.0
        self.backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)
        //        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //添加点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)

    }
    //懒加载试图控件
    //圆圈
    private lazy var backImage :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //遮盖
    private lazy var circleIcon :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子
    private lazy var largeHouse :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //记载中间的那个文案
//    private lazy var tipLable :UILabel = UILabel(text: "关注一些人,回这里看看有什么惊喜",fontSize: 14,textColor: UIColor.lightGrayColor())
    private lazy var tipLable: UILabel = UILabel(text: "关注一些人,回这里看看有什么惊喜", fontSize: 14, textColor: UIColor.lightGrayColor())
    
    //两个按钮
    //登录按钮
    private lazy var loginBtn: UIButton = UIButton(backImageName: "common_button_white_disable", title: "登录", textColor: UIColor.darkGrayColor())
    //注册按钮
    private lazy var registerBtn: UIButton = UIButton(backImageName: "common_button_white_disable", title: "注册", textColor: themeColor)

}
